﻿<?php
// 引用内核启动文件
require 'config.php'; // 引入数据库文件
// 获取当前日期和时间
$currentDateTime = date('Y-m-d H:i:s');

$apMatches = array();
$timeMatches = array();
$timeMatches2 = array();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $mac = $_POST['mac'];
	$ip = filter_input(INPUT_POST, 'ip', FILTER_VALIDATE_IP);
      if ($ip === false) {
    die("无效的IP地址");
        }


    // Convert MAC address to the desired format
    $mac = preg_replace('/[^a-fA-F0-9]/', '', $mac); // Remove non-alphanumeric characters
    $mac = implode('-', str_split($mac, 4)); // Insert hyphens every 4 characters

    // SSH连接参数
    $host = $ip;
    $username = $acuserame;
    $password = $acpassword;

    // 尝试连接SSH（最多重试2次）
    $retryCount = 0;
    while ($retryCount < 3) {
        // SSH连接
        $connection = ssh2_connect($host);
        if ($connection) {
            break; // 如果连接成功，则跳出循环
        }
        
        $retryCount++;
        sleep(1); // 等待1秒后再次尝试连接
    }

    if (!$connection) {
        die("无法建立SSH连接");
    }

    // 用户认证
    if (!ssh2_auth_password($connection, $username, $password)) {
        die("SSH身份验证失败");
    }

    // 打开交互式Shell会话
    $shell = ssh2_shell($connection, 'cli');
    if (!$shell) {
        die("无法打开交互式Shell会话");
    }

    // 发送1命令
    fwrite($shell, "dis station sta-mac $mac\n");

    // 等待命令执行完成
    sleep(1);

    // 读取结果
    $result1 = "";
    while ($line = fgets($shell)) {
        $result1 .= $line;
    }

    // 发送2命令
    fwrite($shell, "dis station offline-record all | include $mac\n");

    // 等待命令执行完成
    sleep(1);

    // 读取结果
    $result2 = "";
    while ($line = fgets($shell)) {
        $result2 .= $line;
    }


    // 关闭连接
    fclose($shell);

    // 提取特定内容
     
    
    $pattern1 = '/AP\s+(?:n|N)ame\s+:\s+(.+)/';
    preg_match($pattern1, $result1, $apMatches);

    $pattern1 = '/Station online time\(ddd:hh:mm:ss\)\s+:\s+(\d{3}:\d{2}:\d{2}:\d{2})/';
    preg_match($pattern1, $result1, $timeMatches);


    $pattern2 = '/AP\s+\d+\s+(.+)\s+\d+\/\d+\s+(\d{4}-\d{2}-\d{2}\/\d{2}:\d{2}:\d{2})$/';
    preg_match($pattern2, $result2, $timeMatches2);



    // 显示结果
    if (count($apMatches) >= 1 && count($timeMatches) >= 2) {

    $AP = $apMatches[1];


    // 使用预处理语句和参数绑定来插入数据
    $stmt = $conn->prepare("INSERT INTO mac_see (ID, AP, MAC, time) VALUES (NULL, ?, ?, NOW())");
    $stmt->bind_param("ss", $AP, $mac);

    if (!$stmt->execute()) {
        die("查询执行失败: " . $stmt->error);
    }

    // 获取插入的自增长ID
    $insertedId = $conn->insert_id;

    // 关闭查询和数据库连接
    $stmt->close();
    $conn->close();
        echo "<pre>";
        echo "查询时间：" . $currentDateTime; // 添加当前日期和时间
        echo "<br>";
        echo "终端当前连接的AP点位：" . $apMatches[1];
        echo "PDA在当前AP在线时长：" . $timeMatches[1];
        echo "<br>";
        echo "</pre>";
    } elseif (count($timeMatches2) >= 2) {
        echo "<pre>";
        echo "查询时间：" . $currentDateTime; // 添加当前日期和时间
        echo "<br>";
        echo "终端最后连接的AP点位：" . $timeMatches2[1];
        echo "<br>";
        echo "PDA连接AP的最后时间：" . $timeMatches2[2];
        echo "<br>";
        echo "</pre>";
    } else {
        echo "<pre>";
        echo "查询时间：" . $currentDateTime; // 添加当前日期和时间
        echo "<br>";
        echo "终端状态：不在线
";
    }

}
?>
